package com.cn.shuangzi.ad.util;

import com.cn.shuangzi.ad.ADApp;
import com.cn.shuangzi.common.SZConst;
import com.cn.shuangzi.util.SZDateUtil;
import com.cn.shuangzi.util.SZUtil;
import com.cn.shuangzi.util.SZXmlUtil;

/**
 * Created by CN.
 */
public class ADUtil extends SZUtil {
    private static final String AD = "ad";
    private static final String USE_TIME = "use_time";

    public static boolean isFirstDayUseAd() {
        long time = new SZXmlUtil(AD).getLong(USE_TIME, 0);
        return isFirstDayUseAd(time);
    }

    public static boolean isFirstDayUseAd(long usedTime) {
        if (usedTime == 0) {
            return true;
        }
        long interval = System.currentTimeMillis() - usedTime;
        if (interval > SZDateUtil.ONE_DAY) {
            return false;
        }
        return true;
    }

    public static void setFirstUseAdTime() {
        SZXmlUtil xmlUtil = new SZXmlUtil(AD);
        long time = xmlUtil.getLong(USE_TIME, 0);
        if (time == 0 || time > System.currentTimeMillis()) {
            xmlUtil.put(USE_TIME, System.currentTimeMillis());
        }
    }

    public static boolean isCanUsePermission(String permission) {
        if (hasPermission(ADApp.getInstance().getApplicationContext(), permission)) {
            if (!isFirstDayUseAd()) {
                return true;
            }
        }
        return false;
    }
}
